package com.uphone.kingmall.activity.personal.set;

import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.bean.ChekBankCardBean;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.utils.AppUtil;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.ToastUtils;
import com.uphone.kingmall.utils.intent.IntentData;
import com.uphone.kingmall.view.SubmitButton0;
import com.uphone.kingmall.view.dialog.OnDialogViewClickListener;
import com.uphone.kingmall.view.dialog.RadishDialog;
import com.uphone.kingmall.wxapi.WXEntryActivity;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/22
 *     desc   :
 * </pre>
 */
public class WalletCashActivity extends BaseActivity {
    @BindView(R.id.tv_wallet)
    TextView tvWallet;
    @BindView(R.id.et_cash)
    EditText etCash;
    @BindView(R.id.rb_ali)
    RadioButton rbAli;
    @BindView(R.id.rb_wchat)
    RadioButton rbWchat;
    @BindView(R.id.rb_union)
    RadioButton rbUnion;
    @BindView(R.id.btn_submit)
    SubmitButton0 btnSubmit;
    @IntentData
    double wallet = 0;
    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_card_id)
    EditText etCardId;
    //    @BindView(R.id.ll_card)
//    LinearLayout llCard;
    @BindView(R.id.rg_pay)
    RadioGroup rgPay;
    @BindView(R.id.ll)
    LinearLayout ll;

    @Override
    public int getLayoutContentId() {
        return R.layout.kingmall_activity_wallet_cash;
    }

    @Override
    public void initView() {
        btnSubmit.setRelaViews(etCash);
        CommonUtil.initTitle(this, "提现", ll);
        rgPay.check(R.id.rb_ali);
//        llCard.setVisibility(View.VISIBLE);
        tvWallet.setText("佣金：" + wallet + "");
    }

    @Override
    public void initData() {
        rgPay.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_ali:
//                        llCard.setVisibility(View.VISIBLE);
                        break;
                    case R.id.rb_wchat:
//                        llCard.setVisibility(View.GONE);
                        break;
                    case R.id.rb_union:
//                        llCard.setVisibility(View.VISIBLE);
                        break;
                }
            }
        });
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        if (CommonUtil.checkViewEmpty("请输入提现金额", etCash)) {
            return;
        }
        String etCashText = etCash.getText().toString().trim();
        HttpParams params = new HttpParams();
        params.put("money", etCashText);
        switch (rgPay.getCheckedRadioButtonId()) {
            case R.id.rb_ali:
                params.put("payType", "1");
                dialogShow(params);

                break;
            case R.id.rb_wchat:
                params.put("payType", "2");
                cashSubmit(params);

                break;
            case R.id.rb_union:
                params.put("payType", "6");
                getBankInfoNet(params);
                break;
        }
    }

    private void dialogShow(final HttpParams params) {
        View dialogView = View.inflate(this, R.layout.kingmall_dialog_alipay_info, null);
        final EditText et_name = dialogView.findViewById(R.id.et_name);
        final EditText et_number = dialogView.findViewById(R.id.et_number);
        new RadishDialog.Builder(this).setView(dialogView)
                .setFromBottom(true)
                .setFullWidth()
                .setClick(R.id.dialog_submit, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(RadishDialog dialog, View view) {
                        if (CommonUtil.checkViewEmpty(new String[]{"请输入真实姓名", "请输入支付宝账号"}, et_name, et_number)) {
                            return;
                        }
                        params.put("trueName", et_name.getText().toString().trim());
                        params.put("cardNo", et_number.getText().toString().trim());
                        cashSubmit(params);
                        KeyboardUtils.hideSoftInput(WalletCashActivity.this);
                        dialog.dismiss();
                    }
                }).show();
    }

    private void getBankInfoNet(final HttpParams cashParams) {
        if (CommonUtil.checkViewEmpty(
                new String[]{"请输入持卡人", "请输入银行卡号"},
                etName, etCardId)) {
            return;
        }
        HttpParams params = new HttpParams();
        params.put("cardNo", etCardId.getText().toString().trim());
        OkGoUtils.normalRequest(MyUrl.checkBankCard, null, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                if (code == 0) {
                    ChekBankCardBean bean = GsonUtils.getGson().fromJson(body, ChekBankCardBean.class);
                    if (bean != null) {
                        if (bean.getResult() != null) {
                            if (!TextUtils.isEmpty(bean.getResult().getBank()) && !TextUtils.isEmpty(bean.getResult().getCardType())) {
                                String tvBankName = bean.getResult().getBank();
                                cashParams.put("trueName", etName.getText().toString().trim());
                                cashParams.put("cardNo", etCardId.getText().toString().trim());
                                cashParams.put("bank", tvBankName);
                                cashSubmit(cashParams);
                                return;
                            }
                        }
                    }
                    ToastUtils.showShortToast(WalletCashActivity.this, "请输入正确的银行卡号");
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    private void cashSubmit(HttpParams params) {

        OkGoUtils.progressRequest(MyUrl.userLingqianTixian, this, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    ToastUtils.showShortToast(WalletCashActivity.this, "申请成功！" + "");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            KeyboardUtils.hideSoftInput(WalletCashActivity.this);
                            WalletCashActivity.this.finish();
                        }
                    }, 500);
                } else if (rgPay.getCheckedRadioButtonId() == R.id.rb_wchat && code == 100) {
                    // 100  未微信号
                    CommonUtil.showAlterDialog(WalletCashActivity.this, "您还没有绑定微信,去绑定？", new CommonUtil.OnAlterDialogCallBack() {
                        @Override
                        public void callBack(RadishDialog dialog, View view) {
                            dialog.dismiss();
                            // 绑定微信
                            try {
                                AppUtil.checkWchat(WalletCashActivity.this, new AppUtil.AppCheckCallback() {
                                    @Override
                                    public void callBack() {
                                        WXEntryActivity.LoginType = 1;
                                        SendAuth.Req req = new SendAuth.Req();
                                        req.scope = "snsapi_userinfo";
                                        req.state = "wechat_sdk_demo_test";
                                        MyApplication.api.sendReq(req);
                                    }
                                });
                            } catch (Exception e) {
                                LogUtils.e("微信绑定异常:" + e.toString());
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }
}
